<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" @click="handleAdd(-1)" type="primary" icon="el-icon-plus">添加</el-button>
      <el-input class="filter-item" @keyup.enter.native="handleFilter" style="width: 200px;" placeholder="请输入关键词" v-model="search.keyword"></el-input>
      <el-button class="filter-item" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <tree-table :data="tableData" :columns="columns" border :expandAll="false" ref="tree">
	    <el-table-column
	      property="navigationUrl"
	      label="链接网址"
	      align="center"
	      show-overflow-tooltip
	      >
	    </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        width="100"
        >
        <template slot-scope="scope">
          <el-tag v-if="!scope.row.isEnable" type="success" size="mini">启用</el-tag>
          <el-tag v-else type="danger" size="mini">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="排序"
        width="180"
        align="center"
      >
        <template slot-scope="scope">
          <el-button size="mini" @click="handleUp(scope.row)">上移</el-button>
          <el-button size="mini" @click="handleDown(scope.row)">下移</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="280"
        align="center"
      >
        <template slot-scope="scope">
          <el-button size="mini" @click="handleAdd(scope.row.navigationId)">添加下级</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)" :disabled="!!scope.row.children.length">删除</el-button>
        </template>
      </el-table-column>
    </tree-table>
    <edit v-if="edit" :data="selectTable" @setData="setData"></edit>
  </div>
</template>

<script>
    import {navList,navRemove,navOrder} from '@/utils/api'
    import waves from '@/directive/waves' // 水波纹指令
    import treeTable from '@/components/TreeTable'
    import edit from './edit'
    export default {
      directives: {
        waves
      },
      data(){
          return {
            columns: [
              {
                text: '名称',
                value: 'navigationName',
                width: 200
              }
            ],
            tableData: [],
            search: {
              keyword: ''
            },
            selection: [],
            selectTable: {},
            pload: false,
            edit: false
          }
      },
      components: {
        edit,
        treeTable
      },
      created(){
         this.getData()
      },
      methods: {
        handleAdd (pid) {
          this.selectTable = {}
          this.selectTable.method = 'add'
          this.selectTable.orderId = 0
          this.selectTable.parentNavigationId  = pid
          this.edit = true
        },
        async handleDelete(index, row) {
          this.$confirm('确认要删除吗',{type:'warning'}).then(async () => {
            try {
              this.pload = false;
              await navRemove({navigationIds: row.navigationId})
              this.$notify({title: '删除成功', type: 'success', duration: 3000})
              let fn = m => {
                m.some(item => {
                  if (item.navigationId === row.parentNavigationId) {
                    item.children.splice(item.children.indexOf(row),1)
                    return true
                  } else {
                    if (item.children.length) {
                      fn(item.children)
                    }
                  }
                })
              }
              
              if (row.parentNavigationId ===-1) {
                this.tableData.splice(this.tableData.indexOf(row),1)
              } else {
                fn(this.tableData)
              }
            } catch(e) {
              this.pload = true
            }
          })
        },
        handleUp(row) {
        	let cur = row._level===1 ? this.tableData : row.parent.children
        	let index = cur.indexOf(row)
          cur[index] = cur.splice(index-1, 1, cur[index])[0];
          this.handleOrder()
        },
        handleDown(row) {
        	let cur = row._level===1 ? this.tableData : row.parent.children
        	let index = cur.indexOf(row)
          cur[index] = cur.splice(index+1 === cur.length ? 0 : index+1, 1, cur[index])[0];
          this.handleOrder();
        },
        async handleOrder() {
          let list = this.$refs.tree.formatData.map((i,index) => {
            return i.navigationId
          }).join(',');
          await navOrder({ids: list})
          this.$notify({title: '排序成功', type: 'success', duration: 1000})
        },
        handleEdit(index, row) {
          this.selectTable = row
          this.selectTable.method = 'edit'
          this.edit = true
        },
        setData(data) {
          console.log(data)
          if (this.selectTable.method === 'add') {
            data.children = []
            if (data.parentNavigationId ===-1) {
              this.tableData.push(data)
            } else {
              let fn = m => {
                m.some(item => {
                  if (item.navigationId === data.parentNavigationId ) {
                    item.children.push(data)
                    return true
                  } else {
                    if (item.children.length) {
                      fn(item.children)
                    }
                  }
                })
              }
              fn(this.tableData)
            }
          } else {
            this.getData()
          }
        },
        async getData(){
          let data = this.JSONo(
            {
            },
            this.search
          )
          this.pload = false
          const Datas = await navList(data)
          this.tableData = Datas
          this.pload = true
        },
        handleFilter () {
          this.getData()
        }
      }
    }
</script>
